#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wave.h"

#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(WaveInfo::instance(), &WaveInfo::signalWaveInfo, this, [this](const QString &s){
        ui->logEdit->appendPlainText(s);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_clrWavBtn_clicked()
{
    ui->wavBox->clear();
}

void MainWindow::on_openBtn_clicked()
{
    QStringList pathList = QFileDialog::getOpenFileNames(nullptr,
                                                u8"文件",
                                                nullptr, //"/",
                                                "audio File(*.wav)");
    if(pathList.isEmpty()) return;

    foreach(QString path, pathList)
        ui->wavBox->addItem(path);
}

void MainWindow::on_mergeBtn_clicked()
{
    int items = ui->wavBox->count();
    if(items <= 1) return;

    QString path = QFileDialog::getSaveFileName(nullptr,
                                                u8"文件",
                                                nullptr, //"/",
                                                "audio File(*.wav)");
    if(path.isEmpty()) return;

    QStringList waveList;
    for(int i = 0; i < items; i++) {
        QString path = ui->wavBox->itemText(i);

        waveList.append(path);
        WaveInfo::instance()->check_waveinfo(path);
    }

    WaveInfo::instance()->merge_wave(path, waveList);
}

void MainWindow::on_clrLogBtn_clicked()
{
    ui->logEdit->clear();
}


void MainWindow::on_wavinfoBtn_clicked()
{
    int items = ui->wavBox->count();
    if(items == 0) return;

    QStringList waveList;
    for(int i = 0; i < items; i++) {
        QString path = ui->wavBox->itemText(i);

        waveList.append(path);
        WaveInfo::instance()->check_waveinfo(path, true);
    }
}

